using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb.Classes;
using YoshiBase.Classes;

namespace YesPlayLove.Clusters
{
    [IgnoreNamespace]
    public class EmailBubbles : EmailBase
    {
        internal EmailMessage EmailMessage;
        internal EmailReply EmailReply;

        public EmailBubbles(EmailCluster cluster)
            : base(cluster)
        {
        }

        public override EmailBase Build(bool active)
        {
            Initialize(active);

            Element.Empty();

            return this;
        }

        public EmailMessage AppendSend(bool active)
        {
            EmailMessage = (EmailMessage)new EmailMessage(EmailCluster).FromHtml("<div/>");
            EmailMessage.HookupCluster(EmailCluster).Build(active).Element.AppendTo(Element).FadeIn(ThemeBase.FadeIn);
            return EmailMessage;
        }

        public EmailReply AppendReply(bool active)
        {
            EmailReply = (EmailReply)new EmailReply(EmailCluster).FromHtml("<div/>");
            EmailReply.HookupCluster(EmailCluster).Build(active).Element.AppendTo(Element).FadeIn(ThemeBase.FadeIn);
            return EmailReply;
        }

        public override EmailBase Fill(object item)
        {
            throw Exceptions.NotImplementedException();
        }

        public EmailBubbles Rebuild(bool active)
        {
            BaseRebuild(active);
            return this;
        }

        public EmailBubbles RebuildMessage(bool active)
        {
            if (EmailMessage != null && EmailMessage.Active != active)
                new Await().AddAw(EmailMessage.RebuildAw, active).Commit();
            return this;
        }

        public void Clear()
        {
            Element.Empty();
            EmailMessage = null;
            EmailReply = null;
        }

        public void ScrollActiveToView()
        {
            if (EmailReply != null && EmailReply.Active)
                EmailReply.ScrollInActiveAsync();
            else if (EmailMessage != null && EmailMessage.Active)
                EmailMessage.ScrollInActiveAsync();
        }

        public void ShowBubbles(string fromId)
        {
            new Await()
                .AddFn(Clear)
                .AddAw(Data.GetConvAw, fromId)
                .HandleDl(Exceptions.BlobLoadError, Await.AbortAw)
                .AddDx(
                    delegate(Await aw)
                    {
                        string myId = Data.My.id;
                        List<EmailItem> cx = (List<EmailItem>) aw[PkgBase.ResultRx];
                        int cnt = 0;
                        bool lastIsOther = false;
                        //jQueryObject lastElement = null;
                        foreach (EmailItem i in cx)
                        {
                            bool atEnd = ++cnt == cx.Count;
                            if (i.toId == myId && i.fromId == fromId)
                            {
                                //lastElement = AppendSend(editing).Fill(i.message).Element;
                                AppendSend(atEnd).Fill(i.message);
                                if (atEnd)
                                    lastIsOther = true;
                            }
                            else
                                AppendReply(false).Fill(i.message);
                        }
                        if (!lastIsOther)
                        {
                            //lastElement = AppendReply(true).Fill("").FocusInput().Element;
                            AppendReply(true).Fill("").FocusInput();
                        }
                        if (EmailMessage != null)
                        {
                            EmailMessage.ScrollInActiveAsync();
                        }
                        if (EmailReply != null)
                        {
                            EmailReply.ScrollInActiveAsync();
                        }
                    })
                .Commit();
        }
    }
}
